package top.someapp.dxf2dsvg.base;

import top.someapp.dxf2dsvg.kits.Numbers;

import javax.annotation.Nonnull;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;

/**
 * @author zwz
 * Created on 2020-04-27
 */
public class ExportOptions {
    public static final String ALL_LAYERS_IN_ONE = "AllLayersInOne";
    public static final String ONE_LAYER_ONE_FILE = "OneLayerOneFile";
    private static final ExportOptions instance = new ExportOptions();

    boolean exportLayer0 = true;
    String layerToSvg = ALL_LAYERS_IN_ONE;
    boolean useInclude = true;
    String[] includes;
    String[] excludes;
    Dimension size;

    private ExportOptions() {
        // single instance
    }

    public static ExportOptions getInstance() {
        return instance;
    }

    public void load(@Nonnull File propsFile) {
        Properties props = new Properties();
        try (FileInputStream ins = new FileInputStream(propsFile)) {
            props.load(ins);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String exportLayer0 = props.getProperty("exportLayer0", "true");
        String layerToSvg = props.getProperty("layerToSvg", ALL_LAYERS_IN_ONE);
        String userInclude = props.getProperty("userInclude", "true");
        String includes = props.getProperty("includes", "");
        String excludes = props.getProperty("excludes", "");
        String size = props.getProperty("excludes", "auto");

        this.exportLayer0 = Boolean.valueOf(exportLayer0);
        if (layerToSvg.equals(ALL_LAYERS_IN_ONE) || layerToSvg.equals(ONE_LAYER_ONE_FILE)) {
            this.layerToSvg = layerToSvg;
        }
        this.useInclude = Boolean.valueOf(userInclude);
        this.includes = includes.split("\n");
        this.excludes = excludes.split("\n");
        if (!"auto".equals(size)) {
            int[] values = Numbers.intValues(size, 'x');
            this.size = new Dimension(values[0], values[1]);
        }
    }

    public boolean isExportLayer0() {
        return exportLayer0;
    }

    public ExportOptions setExportLayer0(boolean exportLayer0) {
        this.exportLayer0 = exportLayer0;
        return this;
    }

    public String getLayerToSvg() {
        return layerToSvg;
    }

    public ExportOptions setLayerToSvg(String layerToSvg) {
        this.layerToSvg = layerToSvg;
        return this;
    }

    public boolean isUseInclude() {
        return useInclude;
    }

    public ExportOptions setUseInclude(boolean useInclude) {
        this.useInclude = useInclude;
        return this;
    }

    private static String join(String[] parts, String connector) {
        if (parts == null || parts.length == 0)
            return "";

        StringBuilder sb = new StringBuilder();
        for (String part : parts) {
            sb.append(part.trim()).append(connector);
        }
        return sb.toString();
    }

    public String[] getIncludes() {
        return includes;
    }

    public String getIncludes(@Nonnull String connector) {
        return join(includes, connector);
    }

    public ExportOptions setIncludes(String[] includes) {
        this.includes = includes;
        return this;
    }

    public String[] getExcludes() {
        return excludes;
    }

    public String getExcludes(String connector) {
        return join(excludes, connector);
    }

    public ExportOptions setExcludes(String[] excludes) {
        this.excludes = excludes;
        return this;
    }

    public Dimension getSize() {
        return size;
    }

    public ExportOptions setSize(Dimension size) {
        this.size = size;
        return this;
    }

    @Override
    public String toString() {
        return "ExportOptions{" +
                "exportLayer0=" + exportLayer0 +
                ", layerToSvg='" + layerToSvg + '\'' +
                ", useInclude=" + useInclude +
                ", includes=" + Arrays.toString(includes) +
                ", excludes=" + Arrays.toString(excludes) +
                ", size=" + size +
                '}';
    }
}
